<?php

namespace App\Http\Controllers;

use App\Transformers\PermissionTransformer;
use Illuminate\Http\Request;
use App\Models\Permission;
use Dingo\Api\Transformer\Factory;
use Illuminate\Validation\Rule;

class PermissionController extends Controller
{
    /**
     * @api {get} /api/permissions?include=children 权限列表
     * @apiGroup Permission
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
            {
                "data": [
                    {
                        "id": 1,
                        "name": null,
                        "parent_id": null,
                        "alias": "组织架构",
                        "created_at": "2018-12-17 13:41:01",
                        "updated_at": "2018-12-17 11:04:42"
                    },
                    {
                        "id": 2,
                        "name": null,
                        "parent_id": null,
                        "alias": "电销管理",
                        "created_at": "2018-12-17 13:41:01",
                        "updated_at": "2018-12-17 11:04:42"
                    },
                ]
            }
     */
    public function index(Request $request,Factory $transformerFactory){
        if ($request->include == 'children') {
            $permissions = Permission::orderBy('sort','ASC')->get()->toTree();
            // 关闭 Dingo 的预加载
            $transformerFactory->disableEagerLoading();
        } else {
            $permissions = Permission::whereIsRoot()->orderBy('sort','ASC')->get();
        }
        if($request->ajax()){
            return $this->response->collection($permissions, new PermissionTransformer());
        }else{
            return $this->response->collection($permissions, new PermissionTransformer());
//            return view('permission.index',['permissions'=>$permissions]);
        }
    }
    public function add(){
        return view('permission.add');
    }
    public function store(Request $request){

        $validatedData = $request->validate([
            'parent_id' => 'nullable|exists:permissions,id|integer',
            'name' => 'nullable|unique:permissions',
            'alias'=>'required|unique:permissions'
        ],[],[
            'alias'=>'中文名',
            'name'=>'权限api'
        ]);

        $node = Permission::create(['name'=>$request->name, 'alias'=>$request->alias, 'guard_name'=>'api']);
        if($parent_id = $request->parent_id){
            $root = Permission::find($parent_id);
            $root->appendNode($node);
        }
        return redirect()->route('permission.index')->with('success', '添加成功！');
    }

    public function edit(Permission $permission){
        return view('permission.edit',['permission'=>$permission]);
    }

    public function update(Request $request, Permission $permission){
        $validatedData = $request->validate([
            'parent_id' => 'nullable|exists:permissions,id|integer',
            'name' => 'nullable|unique:permissions,name,'.$permission->id,
            'alias'=>'required|unique:permissions,alias,'.$permission->id
        ],[],[
            'alias'=>'中文名',
            'name'=>'权限api'
        ]);

        $permission->update($validatedData);

        return redirect()->route('permission.index')->with('success', '修改成功！');
    }


}
